package com.corn.flink.lesson5;

import com.corn.flink.lesson4.Event;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author JimWu
 * @date 2023/3/16 16:04
 **/
public class OutputTagDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> ds = env.socketTextStream("localhost", 1234);
        // 定义侧输出流 用于保存迟到数据
        OutputTag<Event> lateData = new OutputTag<Event>("lateData") {
        };
        SingleOutputStreamOperator<Map<String, Integer>> aggregateDS = ds.map(s -> {
            String[] eventStr = s.split(",");
            return new Event(eventStr[0], Long.valueOf(eventStr[1]));
        })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ZERO)
                                .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                                    @Override
                                    public long extractTimestamp(Event element, long recordTimestamp) {
                                        return element.timestamp;
                                    }
                                }))
                .keyBy(data -> data.type)
                .window(TumblingEventTimeWindows.of(Time.seconds(2)))
                // 设置测输出流
                .sideOutputLateData(lateData)
                .aggregate(new AggregateFunction<Event, Map<String, Integer>, Map<String, Integer>>() {
                    @Override
                    public Map<String, Integer> createAccumulator() {
                        return new HashMap<>();
                    }

                    @Override
                    public Map<String, Integer> add(Event value, Map<String, Integer> accumulator) {
                        Integer amount = Optional.ofNullable(accumulator.get(value.getType())).orElse(0);
                        amount++;
                        accumulator.put(value.getType(), amount);
                        return accumulator;
                    }

                    @Override
                    public Map<String, Integer> getResult(Map<String, Integer> accumulator) {
                        return accumulator;
                    }

                    @Override
                    public Map<String, Integer> merge(Map<String, Integer> a, Map<String, Integer> b) {
                        return null;
                    }
                });
        aggregateDS.print();
        // 拿到迟到数据

        // 获取测输出流 迟到数据可继续操作 返回一个dataStream可以继续操作
        aggregateDS.getSideOutput(lateData)
                .print();

        env.execute();


    }
}
